/*
 * @lc app=leetcode.cn id=2420 lang=golang
 *
 * [2420] 找到所有好下标
 */
package Solutions

// @lc code=start
func goodIndices(nums []int, k int) []int {
	order := make([]int, len(nums))
	reverse := make([]int, len(nums))
	order[0] = 1
	reverse[len(nums)-1] = 1
	for i := 1; i < len(nums); i++ {
		if nums[i] <= nums[i-1] {
			order[i] = order[i-1] + 1
		} else {
			order[i] = 1
		}
	}
	for i := len(nums) - 2; i >= 0; i-- {
		if nums[i] <= nums[i+1] {
			reverse[i] = reverse[i+1] + 1
		} else {
			reverse[i] = 1
		}
	}
	ans := make([]int, 0, len(nums))
	for i := k; i < len(nums)-k; i++ {
		if order[i-1] >= k && reverse[i+1] >= k {
			ans = append(ans, i)
		}
	}
	return ans
}

// @lc code=end
